package com.ruoyi.userr.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.userr.domain.Likes;
import com.ruoyi.userr.service.ILikesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 作品点赞Controller
 * 
 * @author lpt
 * @date 2025-08-13
 */
@RestController
@Controller("userLikesController")
@RequestMapping("/userr/likes")
public class LikesController extends BaseController
{
    @Autowired
    private ILikesService likesService;


    /**
     * 查询作品点赞列表
     */
    @PreAuthorize("@ss.hasPermi('userr:likes:list')")
    @GetMapping("/list")
    public TableDataInfo list(Likes likes)
    {
        startPage();
        List<Likes> list = likesService.selectLikesList(likes);
        return getDataTable(list);
    }

    /**
     * 导出作品点赞列表
     */
    @PreAuthorize("@ss.hasPermi('userr:likes:export')")
    @Log(title = "作品点赞", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Likes likes)
    {
        List<Likes> list = likesService.selectLikesList(likes);
        ExcelUtil<Likes> util = new ExcelUtil<Likes>(Likes.class);
        util.exportExcel(response, list, "作品点赞数据");
    }

    /**
     * 获取作品点赞详细信息
     */
    @PreAuthorize("@ss.hasPermi('userr:likes:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(likesService.selectLikesById(id));
    }

    /**
     * 新增作品点赞
     */
    @PreAuthorize("@ss.hasPermi('userr:likes:add')")
    @Log(title = "作品点赞", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Likes likes)
    {
        return toAjax(likesService.insertLikes(likes));
    }

    /**
     * 修改作品点赞
     */
    @PreAuthorize("@ss.hasPermi('userr:likes:edit')")
    @Log(title = "作品点赞", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Likes likes)
    {
        return toAjax(likesService.updateLikes(likes));
    }

    /**
     * 删除作品点赞
     */
    @PreAuthorize("@ss.hasPermi('userr:likes:remove')")
    @Log(title = "作品点赞", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(likesService.deleteLikesByIds(ids));
    }


    /**
     * 查询指定用户收到的点赞列表（直接返回结果，不封装Result）
     */
    @GetMapping("/author/{authorId}")
    public List<Map<String, Object>> getAuthorLikes(@PathVariable("authorId") Long authorId) {
        if (authorId == null || authorId <= 0) {
            // 若参数无效，可直接抛出异常（Spring会返回500状态码）
            throw new IllegalArgumentException("作者ID无效（不能为空或小于等于0）");
        }
        // 直接返回查询到的点赞列表（Map格式）
        return likesService.getAuthorLikes(authorId);
    }

    // 新增接口：获取点赞总数
    @GetMapping("/count/{authorId}")
    public Integer getAuthorLikeCount(@PathVariable("authorId") Long authorId) {
        if (authorId == null || authorId <= 0) {
            throw new IllegalArgumentException("作者ID无效");
        }
        // 直接返回统计的数量（整数）
        return likesService.getAuthorLikeCount(authorId);
    }
}
